package com.tudouji.common.utils;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.sword.lang.HttpUtils;

import com.alibaba.fastjson.JSONObject;
import com.tudouji.project.system.service.ISysConfigService;

@Service
public class PotatojPostUtils {
	
	@Autowired
	private ISysConfigService configService;
	/**
	 * 
	 * @param map  报文
	 * @param uri  请求路径
	 * @return
	 */
	public Map<String, String> post(Map<String, String> map,String uri){
		Map<String, String> result=new HashMap<>();
		try {
			StringBuilder build = new StringBuilder();
			for (Map.Entry<String, String> en : map.entrySet()) {
				build.append(en.getKey() + "=" + en.getValue());
			}
			System.out.println("=========" + build.toString());
			String token = SecurityUtil.encode(MD5Utils.encryString(build.toString()));
			Map<String, Object> param = new HashMap<>();
			param.put("param", map);
			param.put("token", token);
			String post = JSONObject.toJSONString(param);
			System.out.println("_____post param____________"+post);
			StringBuffer url = new StringBuffer();
			url.append(configService.selectConfigByKey("potatoj_post_uri").concat(uri));
			url.append("param=");
			url.append(URLEncoder.encode(post, "UTF-8"));
			String resu=HttpUtils.get(url.toString());
			result.put("result", resu);
			result.put("param", post);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
